package com.da.lon.wang.xlg.fragment;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.umeng.analytics.MobclickAgent;

@SuppressLint("ResourceAsColor")
public class ClassyFragment extends BaseFragment implements OnClickListener {

    LinearLayout classy_type, classy_brand;
    ImageView classy_type_img, classy_brand_img;
    TextView classy_type_text, classy_brand_text;
    ClassyTypeFragment calssyTypeFragment;
    ClassyBrandFragment calssyBrandFragment;
    int flg;
    private View view;

    @Override
    protected View inflateView(LayoutInflater inflater, ViewGroup var2) {
        view = inflater.inflate(R.layout.ocoffee_classy_fragment, null);
        return view;
    }

    @Override
    protected void initView() {
        initView(view);
        flg = 1;
        clickType();

    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("ClassyFragment");
        MobclickAgent.onResume(getActivity());
    }

    public void onPause() {
        super.onPause();
        calssyTypeFragment = null;
        calssyBrandFragment = null;
        MobclickAgent.onPageEnd("ClassyFragment");
        MobclickAgent.onPause(getActivity());
    }

    private void initView(View view) {
        classy_type = (LinearLayout) view.findViewById(R.id.classy_type);
        classy_brand = (LinearLayout) view.findViewById(R.id.classy_brand);
        classy_type_img = (ImageView) view.findViewById(R.id.classy_type_img);
        classy_brand_img = (ImageView) view.findViewById(R.id.classy_brand_img);
        classy_type_text = (TextView) view.findViewById(R.id.classy_type_text);
        classy_brand_text = (TextView) view
                .findViewById(R.id.classy_brand_text);
        classy_type.setOnClickListener(this);
        classy_brand.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.classy_type:
                clickType();
                break;
            case R.id.classy_brand:
                clickBrand();
                break;
        }
    }


    private void clickType() {
        if (flg != 0) {
            flg = 0;
            classy_type_img
                    .setBackgroundResource(R.drawable.classy_type_clicked);
            classy_type_text.setTextColor(Color.RED);
            classy_brand_img
                    .setBackgroundResource(R.drawable.classy_brand_normal);
            classy_brand_text.setTextColor(Color.BLACK);
            if (calssyTypeFragment == null) {
                calssyTypeFragment = new ClassyTypeFragment();
                addFragment(calssyTypeFragment);
            }
            showFragment(calssyTypeFragment);
        }

    }


    private void clickBrand() {
        if (flg != 1) {
            flg = 1;
            classy_type_img
                    .setBackgroundResource(R.drawable.classy_type_normal);
            classy_type_text.setTextColor(Color.BLACK);
            classy_brand_img
                    .setBackgroundResource(R.drawable.classy_brand_clicked);
            classy_brand_text.setTextColor(Color.RED);
            if (calssyBrandFragment == null) {
                calssyBrandFragment = new ClassyBrandFragment();
                addFragment(calssyBrandFragment);
            }
            showFragment(calssyBrandFragment);
        }

    }


    private void showFragment(Fragment fragment) {
        try {
            FragmentTransaction ft = getActivity().getSupportFragmentManager()
                    .beginTransaction();
            if (calssyTypeFragment != null) {
                ft.hide(calssyTypeFragment);
            }
            if (calssyBrandFragment != null) {
                ft.hide(calssyBrandFragment);
            }
            ft.show(fragment);
            ft.commitAllowingStateLoss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void addFragment(Fragment fragment) {
        FragmentTransaction ft = getActivity().getSupportFragmentManager()
                .beginTransaction();
        ft.add(R.id.classy_content, fragment);
        ft.commit();

    }

    @Override
    protected void RefreshData() {
        // TODO Auto-generated method stub
    }

}
